﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;

namespace HQWeb.Resources
{
    public partial class SelectList : BasePage
    {
         private static string strType;
        protected void Page_Load(object sender, EventArgs e)
        {
            //VerdictIsLogin();
            try
            {

                if (!Page.IsPostBack)
                {
                    this.Material_ID.Value = Session["MaterialID"].ToString();
                    this.Material_Name.Value = Session["MaterialName"].ToString();
                    this.Param1.Value = Session["Param"].ToString();
                    string strId = Request["pid"];
                    strType = "'" + strId + "'" + ",";
                    GetAllType(strId);
                    txtPIdHid.Text = strType.Substring(0, strType.Length - 1);
                    BindList();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        #region 获得所有下级类型
        private void GetAllType(string strID)
        {
            GetChild(strID);
        }
        #endregion

        #region 获取孩子节点
        private void GetChild(string strID)
        {
            BLL.SysManage.T_PUB_MATERIALTYPE bll = new BLL.SysManage.T_PUB_MATERIALTYPE();
            List<Model.SysManage.T_PUB_MATERIALTYPE> modelist = new List<Model.SysManage.T_PUB_MATERIALTYPE>();
            string strWhere = string.Format(" PARENT_ID = {0}", Convert.ToInt32(strID));
            modelist = bll.GetModelList(strWhere);
            foreach (Model.SysManage.T_PUB_MATERIALTYPE item in modelist)
            {
                strType += "'" + item.TYPE_ID + "'" + ",";
                GetChild(item.TYPE_ID.ToString());
            }
        }
        #endregion

        #region 绑定列表数据
        private void BindList()
        {
            BLL.SysManage.T_PUB_MATERIAL bll = new BLL.SysManage.T_PUB_MATERIAL();
            string strIDs = Session["Param"].ToString();
            GridView1.DataSource = bll.GetMaterialInfo(this.txtPIdHid.Text, this.txtName.Text, strIDs);
            GridView1.DataBind();
        }
        #endregion

        #region 翻页事件
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                //strWhere = strWhere.Replace("order by operatetime DESC", "");
                int newPageIndex = 0;

                if (-2 == e.NewPageIndex)
                { // when click the "GO" Button
                    TextBox txtNewPageIndex = null;
                    GridViewRow pagerRow = GridView1.Controls[0].Controls[GridView1.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
                    //GridViewRow pagerRow = GridView1.BottomPagerRow; //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
                    if (null != pagerRow)
                    {
                        txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
                    }

                    if (null != txtNewPageIndex)
                    {
                        newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
                    }

                    newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
                    newPageIndex = newPageIndex >= GridView1.PageCount ? GridView1.PageCount - 1 : newPageIndex;
                    GridView1.PageIndex = newPageIndex;
                }
                else if (e.NewPageIndex == -1)
                {
                    newPageIndex = 0;
                    GridView1.PageIndex = 0;
                }
                else
                {  // when click the first, last, previous and next Button
                    newPageIndex = e.NewPageIndex;
                    GridView1.PageIndex = e.NewPageIndex;
                }

                // check to prevent form the NewPageIndex out of the range


                BindList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 选择
        protected void btnDelServer_Click(object sender, EventArgs e)
        {
            string strSelects = this.strID.Text;
            int intRow = 0;
            string strRowsID = "";
            string strRowsName = "";
            intRow = Convert.ToInt32(strSelects);
            strRowsID += GridView1.DataKeys[intRow].Value.ToString();
            strRowsName += GridView1.Rows[intRow].Cells[1].Text.ToString();
            string strJS = "SelectClick('" + this.Material_Name.Value + "','" + strRowsName + "','" + this.Material_ID.Value + "','" + strRowsID + "')";
            Universal.JS.RunJS(strJS);
        }
        #endregion

        protected void tbnSearch_Click(object sender, EventArgs e)
        {
            BindList();
        }
    }
}